package com.xidian.zhiancloud.service.dangerWorkTicket.WorkManager;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xidian.zhiancloud.dao.MyDelayApplicationMapper;
import com.xidian.zhiancloud.dao.syset.SysPositionMapper;
import com.xidian.zhiancloud.dao.syset.SysUserMapper;
import com.xidian.zhiancloud.dao.syset.usermanage.OrganizationMapper;
import com.xidian.zhiancloud.dao.workTicket.WorkTicketMapper;
import com.xidian.zhiancloud.dao.workTicket.WorkTicketTemplateMapper;
import com.xidian.zhiancloud.enums.GeneralResponseEnums;
import com.xidian.zhiancloud.exception.CustomizeException;
import com.xidian.zhiancloud.model.DelayApplicationApprove;
import com.xidian.zhiancloud.model.syset.Organization;
import com.xidian.zhiancloud.model.syset.SysPosition;
import com.xidian.zhiancloud.model.syset.SysUser;
import com.xidian.zhiancloud.model.dangerWorkTicket.WorkTicket;
import com.xidian.zhiancloud.model.dangerWorkTicket.WorkTicketTemplate;
import com.xidian.zhiancloud.qo.dangerWorkTicket.WorkTicketQO;
import com.xidian.zhiancloud.vo.dangerWorkTicket.DelayApplicationApproveVO;
import com.xidian.zhiancloud.vo.dangerWorkTicket.MyDelayApplicationDTOVO;
import com.xidian.zhiancloud.vo.dangerWorkTicket.WorkTicketVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


@Service
public class MyDelayApplicationService extends ServiceImpl<MyDelayApplicationMapper, DelayApplicationApprove> implements IService<DelayApplicationApprove> {
    @Autowired
    private WorkTicketMapper workTicketMapper;

    @Autowired
    private MyDelayApplicationMapper myDelayApplicationMapper;

    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private SysPositionMapper sysPositionMapper;

    @Autowired
    private OrganizationMapper organizationMapper;

    @Autowired
    private WorkTicketTemplateMapper workTicketTemplateMapper;

    public MyDelayApplicationDTOVO getVOById(Long id) {
        // 获取申请延期的作业票内容
        WorkTicket workTicket = workTicketMapper.selectById(id);
        if(workTicket == null) {
            throw new CustomizeException(GeneralResponseEnums.SYSTEM_ERROR);
        }

        // 获取对给定id作业票中，关于作业审批的记录
        LambdaQueryWrapper<DelayApplicationApprove> query = Wrappers.<DelayApplicationApprove>lambdaQuery();
        query.eq(DelayApplicationApprove::getRelateWorkId, id);
        List<DelayApplicationApprove> delayApplicationApproves =  myDelayApplicationMapper.selectList(query);

        // 新建DTOVO对象，用于存储整个返回值
        MyDelayApplicationDTOVO res = new MyDelayApplicationDTOVO();
        // 作业票部分
        WorkTicketVO vo = new WorkTicketVO();
        BeanUtils.copyProperties(workTicket, vo);

        List<DelayApplicationApproveVO> vo1 = new ArrayList<DelayApplicationApproveVO>();
        // 审批记录部分
        for (int i = 0; i < delayApplicationApproves.size(); i++) {
            // 新建VO对象，不能一次将整个父类List<>的属性复制给另一个子类List<>
            DelayApplicationApproveVO delayApplicationApproveVO = new DelayApplicationApproveVO();
            // 将对应位置的父类unsafeWorkApproves对象的属性，复制给其子类VO对象
            BeanUtils.copyProperties(delayApplicationApproves.get(i), delayApplicationApproveVO);
            // vo1新加入VO对象
            vo1.add(delayApplicationApproveVO);
            // 联表查询用户对象
            Long approverId = vo1.get(i).getApproverId();
            SysUser approver =  sysUserMapper.selectById(approverId);
            vo1.get(i).setApproverName(approver == null ? "该用户不存在" : approver.getName());
            // 获取审批人所在部门
            Organization organization = organizationMapper.selectById(vo1.get(i).getApproveOrganizationId());
            vo1.get(i).setApproveOrganizationName(organization == null ? "该部门已被删除" : organization.getName());
        }

        // 将作业票的id改为String类型
        SysUser creator = sysUserMapper.selectById(vo.getCreatorId());
        vo.setCreatorName(creator == null ? "该用户不存在" : creator.getName());
        SysPosition position = sysPositionMapper.selectById(vo.getPositionId());
        vo.setLocationName(position == null ? "该位置不存在" : position.getName());
        String template = workTicketTemplateMapper.selectById(vo.getTicketType()).getName();
        vo.setWorkTemplateName(template == null ? "该作业票不存在" : template);


        res.setWorkTicketVO(vo);
        res.setDelayApplicationApproveVOList(vo1);
        return res;
    }

    public IPage<WorkTicketVO> getListByQO(WorkTicketQO workTicketQO) {
        Page<WorkTicketVO> page = new Page<>(workTicketQO.getCurrent(), workTicketQO.getSize());
        OrderItem orderItem = new OrderItem();
        // 排序
        orderItem.setColumn("create_time").setAsc(false);
        page.getOrders().add(orderItem);

        IPage<WorkTicketVO> res = workTicketMapper.getListByQO(page, workTicketQO);
        // 联表查询
        res.getRecords().forEach(
                vo -> {
                    Long ticketType = vo.getTicketType();
                    WorkTicketTemplate template = workTicketTemplateMapper.selectById(ticketType);
                    vo.setWorkTemplateName(
                            template == null ? "作业票类型已删除" : template.getName()
                    );
                    SysPosition position = sysPositionMapper.selectById(vo.getPositionId());
                    vo.setLocationName(position == null ? "该位置不存在" : position.getName());
                }
        );
        return res;
    }
}
